<?php
/**
 * Created by PhpStorm.
 * User: fangwei
 * Date: 2019-08-21
 * Time: 16:54
 */

namespace App\Http\Repositories\Daos\Impls;


use App\Http\Repositories\Daos\IProductDao;
use App\Http\Repositories\Eloquent\Product;

class ProductDao extends BaseDao implements IProductDao
{
    public function __construct(Product $model)
    {
        parent::__construct($model);
    }

    public function lists($search, $page, $size)
    {
        $filterMap = $this->model->newQuery();
        if (isset($search['title']) && !empty($search['title'])) {
            $filterMap->where('title','like','%'.$search['title'].'%');
        }
        if (isset($search['category_id']) && !empty($search['category_id'])) {
            if (is_array($search['category_id'])) {
                $filterMap->whereIn('category_id',$search['category_id']);
            }else{
                $filterMap->where('category_id',$search['category_id']);
            }
        }
        if (isset($search['state']) && $search['state'] !== '' && $search['state'] !== null) {
            $filterMap->where('state',$search['state']);
        }
        $filterMap->where('is_deleted',0);
        return $this->page($filterMap,$page,$size);
    }

    public function byProduct($search)
    {
        $filterMap = $this->model->newQuery();
        $filterMap->where('brand_id', $search['brand_id']);
        $filterMap->where('brand_model_id', $search['brand_model_id']);
        if (isset($search['year']) && $search['year']) {
            $filterMap->where('year', $search['year']);
        }
        if (isset($search['product_model']) && $search['product_model']) {
            $filterMap->where('product_model', $search['product_model']);
        }
        $filterMap->where('is_deleted',0);
        return $filterMap->first();
    }
}